package ul.elab;


import java.{io};
import java.{util};
import java.{awt};
import javax.swing.{BoxLayout, KeyStroke, filechooser, DefaultComboBoxModel};
import scala.{swing};
import ul.{audio};


object ConfigPanel extends swing.GridBagPanel {

    val tr: (String)=>String = Config.tran.tr _;

    val langComboBox = new swing.ComboBox( Config.tran.displayLangs );
    if (Config.tran.langs.contains( Config("lang").s ))
        langComboBox.selection.index = Config.tran.langs.indexOf( Config("lang").s );

    val mixersNames = audio.Audio.mixersNames;
    val sourceMixer = audio.Audio.mixer( Config("sourceMixer").s );
    val sourceLineComboBox = new swing.ComboBox( List("") ) {
        peer.setModel(new DefaultComboBoxModel);
    };
    val sourceMixerComboBox = new swing.ComboBox( "" :: audio.Audio.mixersNames.toList ) {
    };
    if (!sourceMixer.isEmpty) {
        sourceMixerComboBox.selection.index = audio.Audio.mixersNames.indexOf( Config("sourceMixer").s ) + 1;
        sourceLineComboBox.peer.removeAllItems;
        val sourceDataLines = "" :: (audio.Audio.sourceDataLines(sourceMixer.get).map((sl) => sl.getLineInfo.toString).toList);
        sourceDataLines.foreach( (sl) => sourceLineComboBox.peer.addItem( sl ) );
        if (sourceDataLines.contains(Config("sourceLine").s))
            sourceLineComboBox.selection.index = sourceDataLines.indexOf(Config("sourceLine").s);
    };
    
    val sourceFreqs = List( 22050, 44100, 48000, 96000, 192000 );
    val sourceFreqComboBox = new swing.ComboBox( sourceFreqs ) {
        selection.index = sourceFreqs.indexOf( Config("sourceFreq").i );
    };
    
    add(
        new swing.Label( "language: " ),
        new this.Constraints( 0,0, 1,1, 0.1,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.None.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );
    add(
        langComboBox,
        new this.Constraints( 1,0, 1,1, 1.0,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.Horizontal.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );

    add(
        new swing.Label( tr("source mixer: ") ),
        new this.Constraints( 0,1, 1,1, 0.1,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.None.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );
    add(
        sourceMixerComboBox,
        new this.Constraints( 1,1, 1,1, 1.0,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.Horizontal.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );

    add(
        new swing.Label( tr("source line: ") ),
        new this.Constraints( 0,2, 1,1, 0.1,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.None.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );
    add(
        sourceLineComboBox,
        new this.Constraints( 1,2, 1,1, 1.0,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.Horizontal.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );

    add(
        new swing.Label( tr("source frequency: ") ),
        new this.Constraints( 0,3, 1,1, 0.1,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.None.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );
    add(
        sourceFreqComboBox,
        new this.Constraints( 1,3, 1,1, 1.0,1.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.Horizontal.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );

    // vertical glue
    add(
        new swing.Label( "" ),
        new this.Constraints( 0,4, 2,1, 1.0,100.0,
            swing.GridBagPanel.Anchor.Center.id,
            swing.GridBagPanel.Fill.Both.id,
            new awt.Insets(0,0,0,0),
            0,0
        )
    );
    
    listenTo(sourceMixerComboBox.selection, sourceLineComboBox.selection, sourceFreqComboBox.selection);
    reactions += {
        case swing.event.SelectionChanged(`sourceMixerComboBox`) =>
            sourceLineComboBox.peer.removeAllItems;
            val m = audio.Audio.mixer( sourceMixerComboBox.selection.item );
            if (!m.isEmpty) {
                for ( sl <- ("" :: (audio.Audio.sourceDataLines(m.get).map((sl) => sl.getLineInfo.toString).toList)) )
                    sourceLineComboBox.peer.addItem( sl );
                Config("sourceMixer").s = m.get.getMixerInfo.toString;
                Config("sourceLine").s = "";
            } else {
                Config("sourceMixer").s = "";
                sourceLineComboBox.peer.addItem( "" );
            }
        case swing.event.SelectionChanged(`sourceLineComboBox`) =>
            Config("sourceLine").s = "";
            val m = audio.Audio.mixer( sourceMixerComboBox.selection.item );
            if (!m.isEmpty) {
                val sl = audio.Audio.sourceDataLine(m.get, sourceLineComboBox.selection.item);
                if (!sl.isEmpty) Config("sourceLine").s = sl.get.getLineInfo.toString;
            }
        case swing.event.SelectionChanged(`sourceFreqComboBox`) =>
            Config("sourceFreq").i = sourceFreqComboBox.selection.item;
    }

}

